<template>
<div>
  <slot></slot>
</div>
</template>

<script>
export default {
  componentName: 'KFrom',
  provide() {
    return {
      form: this,
    };
  },
  props: {
    model: {
      type: Object,
      required: true,
    },
    rules: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data () {
    return {
      field: []
    }
  },
  created() {
    this.$on('kform.addField', (field) => {
      if (field) {
        this.field.push(field)
      }
    })
  },
  methods: {
    validate(cb) {
      // 获取所有孩子kFormItem
      // 返回一个[promise, promise]
      console.log('this.field', this.field)
      const tasks = this.field.map(item => item.validate());
      // 统一处理所有结果
      Promise.all(tasks)
        .then(() => {
          cb(true)
        })
        .catch(() => {
          cb(false)
        })
    },
  },
};
</script>
